//
//  ProcessView.m
//  Image
//
//  Created by zxs on 2017/6/1.
//  Copyright © 2017年 zxs. All rights reserved.
//

#import "ProcessView.h"

@interface ProcessView ()

@property (nonatomic, assign) CGFloat progress;

@end

@implementation ProcessView

- (void)updateProcess:(CGFloat)process {
    if (process < 0 || (process > 1)) {
        return;
    }
    self.progress = process;
    [self setNeedsDisplay];
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
    
    // 画圆弧
    CGFloat startA = - M_PI_2;
    CGFloat endA =  - M_PI_2 + self.progress * M_PI * 2;
    NSLog(@"%f - %f - %f",self.progress,(3.14159265359*self.progress)/180,endA);
    UIBezierPath * path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.bounds.size.width*0.5, self.bounds.size.height*0.5) radius:self.bounds.size.width*0.5-10 startAngle:startA endAngle:endA clockwise:YES];
    UIColor *color = [UIColor colorWithRed:self.progress green:(1-self.progress) blue:0 alpha:1];
    [color setStroke];
    path.lineWidth = 5;
    [path stroke];
    
    // 画文字
    //1.获取当前上下文
    CGContextRef contextRef = UIGraphicsGetCurrentContext();
    //2.创建文字
    NSString * str = [NSString stringWithFormat:@"%04.2f%%", (self.progress * 100)];
    //设置字体样式
    NSMutableDictionary * dict = [NSMutableDictionary dictionary];
    //NSFontAttributeName:字体大小
    dict[NSFontAttributeName] = [UIFont systemFontOfSize:20];
    //字体前景色
    dict[NSForegroundColorAttributeName] = color;
    //字体背景色
    dict[NSBackgroundColorAttributeName] = [UIColor clearColor];
    //字体阴影
    NSShadow * shadow = [[NSShadow alloc]init];
    //阴影偏移量
    shadow.shadowOffset = CGSizeMake(2, 2);
    //阴影颜色
    shadow.shadowColor = [UIColor greenColor];
    //高斯模糊
    shadow.shadowBlurRadius = 5;
    dict[NSShadowAttributeName] = shadow;
    //字体间距
    dict[NSKernAttributeName] = @10;
    //绘制到上下文
    //从某一点开始绘制 默认 0 0点
    //[str drawAtPoint:CGPointMake(100, 100) withAttributes:dict];
    //绘制区域设置
    CGSize size = [str sizeWithAttributes:dict];
    CGFloat width = size.width;
    CGFloat height = size.height;
    CGFloat x = rect.size.width / 2 - width / 2;
    CGFloat y = rect.size.height / 2 - height / 2;
    [str drawInRect:CGRectMake(x, y, width, height) withAttributes:dict];
    //添加到上下文
    CGContextStrokePath(contextRef);
}

@end
